package com.lgx.mobile.rest.gift;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lgx.common.exception.DataException;
import com.lgx.mobile.entity.Gift;
import com.lgx.mobile.service.IGiftService;

/**
 * 
 * @author xuyichen
 *
 */
@Controller
@RequestMapping("/updateGift")
public class UpdateGiftController {
	
	private static final Logger logger = Logger.getLogger(UpdateGiftController.class);

	@Autowired
	IGiftService giftService;
	
	public IGiftService getGiftService() {
		return giftService;
	}

	/**
	 * 礼品变更
	 * @param giftId id is key
	 * @param name name is key
	 * @return 0：失败；1：成功；2：礼品不存在；3：参数异常，有参数为空
	 */
	@RequestMapping(value="/{id}/{name}",method=RequestMethod.GET)
	@ResponseBody
	public Object updateGift(@PathVariable("id") String giftId,@PathVariable("name") String name){
		if(StringUtils.isNotBlank(giftId)&&StringUtils.isNotBlank(name)){
			Gift gift = getGiftService().findGiftById(Long.parseLong(giftId));
			if(gift!=null){
				String status = "0";
				gift.setName(name);
				try {
					getGiftService().updateGift(gift);
					status = "1";
				} catch (DataException e) {
					e.printStackTrace();
				}
				return status;
			}else{
				if(logger.isDebugEnabled()){
					logger.debug("updateGift faild giftId or name is error");
				}
//				"updateGift faild please check giftId is exists";
				return "2";
			}
		}else{
			if(logger.isDebugEnabled()){
				logger.debug("updateGift faild giftId or name is not filled");
			}
//		    "updateGift faild please check giftId or name is filled";
			return "3";
		}
	}
}
